/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hisense.settings.picture;
import com.hisense.settings.util.Wrapper;
import com.android.settings.R;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.IPowerManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.Preference.BaseSavedState;
import android.preference.SeekBarDialogPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;

import android.util.Log;


public class ContrastPreference extends SeekBarDialogPreference implements
        SeekBar.OnSeekBarChangeListener{

	private static final String TAG = "ContrastPreference";

    private SeekBar mSeekBar;
	private TextView mContrastNumView;
    private int mOldContrast;

    private boolean mRestoredContrastOldState;

    // Contrast range is from 0 - 100. Need to make sure that user
    // doesn't set the backlight to 0 and get stuck
    private static final int MINIMUM_CONTRAST = 0;
    private static final int MAXIMUM_CONTRAST = 100;

    private ContentObserver mContrastObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            //onContrastChanged();
        }
    };


    public ContrastPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.preference_dialog_contrast);
        setDialogIcon(R.drawable.ic_settings_display);
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        //getContext().getContentResolver().registerContentObserver(
        //        Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), true,
        //        mBrightnessObserver);

        mRestoredContrastOldState = false;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);

        mSeekBar = (SeekBar)view.findViewById(R.id.contrast_seekbar);
        mSeekBar.setMax(MAXIMUM_CONTRAST - MINIMUM_CONTRAST);
		mSeekBar.setKeyProgressIncrement(1);		
        mOldContrast = getContrast();
        mSeekBar.setProgress(mOldContrast - MINIMUM_CONTRAST);
        mSeekBar.setOnSeekBarChangeListener(this);

		mContrastNumView = (TextView)view.findViewById(R.id.contrast_value);
		mContrastNumView.setText(""+mOldContrast);		
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromTouch) {
        setContrast(progress);	
		mContrastNumView.setText(""+progress);
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        // NA
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        // NA
    }

    private int getContrast() {
        int contrast = 0;
		contrast = Wrapper.getWrapperInstance().getContrast();
        return contrast;
    }


    private void onContrastChanged() {
        int contrast = getContrast();
        mSeekBar.setProgress(contrast - MINIMUM_CONTRAST);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        final ContentResolver resolver = getContext().getContentResolver();
        if (positiveResult) {
            //Settings.System.putInt(resolver,
            //        Settings.System.SCREEN_CONTRAST,
            //        mSeekBar.getProgress() + MINIMUM_CONTRAST);
        } else {
            restoreContrastOldState();
        }
        
		//Wrapper.getWrapperInstance().setPictureMode(SCMPQSettingsEnums.SCMPictureModes.SCM_PICTURE_MODE_PRO1.ordinal());
		callChangeListener(getContrast());
        resolver.unregisterContentObserver(mContrastObserver);
    }

    private void restoreContrastOldState() {
        if (mRestoredContrastOldState) return;

        setContrast(mOldContrast);
        mRestoredContrastOldState = true;
    }

    private void setContrast(int contrast) {
		Wrapper.getWrapperInstance().setContrast(contrast);
    }


    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
        if (getDialog() == null || !getDialog().isShowing()) return superState;

        // Save the dialog state
        final SavedState myState = new SavedState(superState);
        myState.progress = mSeekBar.getProgress();
        myState.oldProgress = mOldContrast;

        // Restore the old state when the activity or dialog is being paused
        restoreContrastOldState();
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        mOldContrast = myState.oldProgress;
        setContrast(myState.progress + MINIMUM_CONTRAST);
    }

    private static class SavedState extends BaseSavedState {

        int progress;
        int oldProgress;

        public SavedState(Parcel source) {
            super(source);
            progress = source.readInt();
            oldProgress = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(progress);
            dest.writeInt(oldProgress);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {

            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}

